﻿using Microsoft.Xna.Framework;

namespace FlipRunner.Physics
{
    public struct CartesianRectangle
    {
        /// <summary>
        /// X coordinate of Bottom Left Corner
        /// </summary>
        public float X;

        /// <summary>
        /// Y coordinate of Bottom Left Corner
        /// </summary>
        public float Y;

        public float Width;

        public float Height;

        public CartesianPoint Position { get { return new CartesianPoint(X, Y);}}

        public float Right
        {
            get { return X + Width; }
        }

        public float Left
        {
            get { return X; }
        }

        public float Top
        {
            get { return Y + Height; }
        }

        public float Bottom
        {
            get { return Y; }
        }

        public CartesianRectangle(float x, float y, float width, float height)
        {
            X = x;
            Y = y;
            Width = width;
            Height = height;
        }

        /// <summary>
        /// Creates a new CartesianRectangle from a Point and Size
        /// </summary>
        /// <param name="position">Bottom Left Corner of the Rectangle</param>
        /// <param name="size">Rectangle's size</param>
        public CartesianRectangle(CartesianPoint position, ISize size) : this(position.X, position.Y, size.Width, size.Height)
        {
        }

        public static CartesianRectangle FromCenter(CartesianPoint center, ISize size)
        {
            CartesianVector vector = (CartesianVector) size;
            return new CartesianRectangle(center - vector/2, size);
        }

        public static CartesianRectangle FromTopLeft(CartesianPoint topLeft, ISize size)
        {
            return new CartesianRectangle(topLeft.X, topLeft.Y - size.Height, size.Width, size.Height);
        }

        public Rectangle ToPixels(float pixelsPerMeter, int worldPixelHeight)
        {
            Point pixelTopLeft = new CartesianPoint(Left, Top).ToPixels(pixelsPerMeter, worldPixelHeight);
            int width = Width.MetersToPixels(pixelsPerMeter);
            int height = Height.MetersToPixels(pixelsPerMeter);

            return new Rectangle(pixelTopLeft.X, pixelTopLeft.Y, width, height);
        }
    }
}